#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,fa[N],u[N],v[N],id[N],siz[N],vis[N],ans;
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
void join(int x,int y){
	fa[find(x)]=find(y);
}
signed main() {
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>u[i]>>v[i];
	for(int i=1;i<=n+n;i++) fa[i]=i;
	for(int i=1;i<=n;i++) join(u[i],v[i]);
	for(int i=1;i<=n+n;i++) ++siz[find(i)];
	for(int i=1;i<=n;i++) ++id[find(u[i])];
	for(int i=1;i<=n+n;i++){
		int x=find(i);
		if(!vis[x]) vis[x]=1,ans+=min(siz[x],id[x]);
	}
	cout<<ans;
	return 0;
}
